import java.awt.GridLayout;

import java.awt.Label;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.net.URL;

import java.net.URLConnection;

import javax.swing.*;

	public class UploadForm extends JFrame implements ActionListener {

		File image;

		JTextField server = new JTextField("");

		JTextField username = new JTextField("");

		JTextField imagePath = new JTextField("");

		JButton imageBrowse = new JButton("Find Image");

		JPasswordField password = new JPasswordField("");

		JButton uploadButton = new JButton("Upload");

		public void actionPerformed(ActionEvent ae) {
			if (ae.getSource().equals(imageBrowse)) {

				String s;

				JFileChooser jfc = new JFileChooser();

				jfc.showDialog(jfc, "Choose an image file");

				s = jfc.getSelectedFile().getAbsolutePath();

				imagePath.setText(s);

				} else

				if (ae.getSource().equals(uploadButton)) {

				try {

				File f = new File(imagePath.getText());
				String urlString = "ftp://" + username.getText() + ":" + password.getText() + "@" + server.getText() + "/" + f.getName();
				
				URL url = new URL(urlString);

				URLConnection connection = url.openConnection();

				connection.setDoOutput(true);

				BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());

				FileInputStream in = new FileInputStream(f);

				byte[] buffer = new byte[1024];

				int i = 0;

				while ((i = in.read(buffer)) >= 0) {

				out.write(buffer, 0, i);

				}

				out.close();

				in.close();

				}catch (Exception e) {

				System.out.println(e.getMessage());

				}

				}

		}

		public UploadForm() {
			imageBrowse.addActionListener(this);

			uploadButton.addActionListener(this);

			this.setSize(640,100);

			this.setLayout(new GridLayout(0,2));

			this.add(imagePath);

			this.add(imageBrowse);

			this.add(uploadButton);


		}

		public static void main(String[] args) {
			JFrame f = new UploadForm();

			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

			f.setVisible(true);
			
		}

		}

		


